/*
 *  Copyright 2019-2020 Zheng Jie
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package com.fzhucc.modules.pms.controller

import com.fzhucc.annotation.Log
import com.fzhucc.annotation.rest.AnonymousGetMapping
import com.fzhucc.annotation.rest.AnonymousPostMapping
import com.fzhucc.base.PageDTO
import com.fzhucc.modules.pms.domain.Project
import com.fzhucc.modules.pms.domain.dto.*
import com.fzhucc.modules.pms.service.ProjectService
import com.fzhucc.modules.pms.service.ProjectSmallService
import com.fzhucc.utils.JwtSecurityUtils
import io.swagger.annotations.Api
import io.swagger.annotations.ApiOperation
import lombok.RequiredArgsConstructor
import org.springframework.data.domain.Pageable
import org.springframework.security.access.prepost.PreAuthorize
import org.springframework.validation.annotation.Validated
import org.springframework.web.bind.annotation.*
import javax.servlet.http.HttpServletResponse

/**
 * @author Woolsen
 * @date 2022-11-17
 */
@RestController
@RequiredArgsConstructor
@Api(tags = ["项目库管理"])
@RequestMapping("/api/project")
class ProjectController(
    private val projectService: ProjectService,
    private val projectSmallService: ProjectSmallService,
) {

    @Log("导出数据")
    @ApiOperation("导出数据")
    @AnonymousGetMapping(value = ["/download"])
    //@GetMapping(value = ["/download"])
    //@PreAuthorize("@el.check('project:list')")
    fun exportProject(response: HttpServletResponse, criteria: ProjectQueryCriteria) {
        criteria.delFlag = "0"
        projectService.download(projectService.queryAll(criteria), response)
    }

    @Log("导出所有数据")
    @ApiOperation("导出所有数据")
    @AnonymousGetMapping(value = ["/AllDownload"])
    fun exportAllProject(response: HttpServletResponse) {
        projectService.download(projectService.queryAllNormal(), response)
    }

    @Log("同步数据")
    @ApiOperation("同步数据")
    @PostMapping("/sync")
    @PreAuthorize("@el.check('project:sync')")
    fun syncProject() {
        projectService.syncFromSci()
    }


    @Log("同步五小数据")
    @ApiOperation("同步五小数据")
    @PostMapping("/syncWx")
    @PreAuthorize("@el.check('project:sync')")
    fun syncWxProject() : String {
        return projectService.syncFromSciWx()
    }


    @AnonymousGetMapping
    @Log("查询项目库")
    @ApiOperation("查询项目库")
    fun queryAll(criteria: ProjectQueryCriteria, pageable: Pageable): Map<String, Any> {
        criteria.delFlag = "0"
        if (JwtSecurityUtils.hasAuthority("project:list")) {
            return projectService.queryAll(criteria, pageable)
        } else {
            criteria.isShow = true
            return projectSmallService.queryAllXcx(criteria, pageable)
        }
    }

    /*@GetMapping("/showIndexList")
    @Log("管理端查询项目库")
    @ApiOperation("管理端查询项目库")
    fun showIndexList(criteria: ProjectQueryCriteria, pageable: Pageable): Map<String, Any> {
        criteria.delFlag = "0"
        if (JwtSecurityUtils.hasAuthority("project:list")) {
            return projectService.queryAll(criteria, pageable)
        } else {
            criteria.isShow = true
            return projectSmallService.queryAllXcx(criteria, pageable)
        }
    }*/

    @AnonymousGetMapping("/queryLargeProList")
    @Log("查询首页大屏项目库")
    @ApiOperation("查询首页大屏项目库")
    fun queryLargeProAll(criteria: ProjectQueryCriteria, pageable: Pageable): Map<String, Any> {
        return projectService.queryAllWithOutDetails(criteria, pageable)
    }

    @AnonymousGetMapping("/queryLargeProListByOrgArea")
    @Log("查询首页大屏项目库byOrgArea")
    @ApiOperation("查询首页大屏项目库byOrgArea")
    fun queryLargeProListByOrgArea(criteria: ProjectQueryCriteria, pageable: Pageable): Map<String, Any> {
        return projectService.queryAllWithOutDetailsByOrgArea(criteria, pageable)
    }

    @AnonymousGetMapping("queryShowedCount")
    @Log("微信首页查询项目库数量")
    @ApiOperation("微信首页查询项目库数量")
    fun queryShowedCount(level: Int): Int {
        return projectService.queryByLevel(level)
    }

    @AnonymousGetMapping("queryShowedCountByOrgArea")
    @Log("微信首页查询项目库数量byOrgArea")
    @ApiOperation("微信首页查询项目库数量byOrgArea")
    fun queryShowedCountByOrgArea(level: Int,orgArea: String): Int {
        return projectService.queryByLevelByOrgCode(level,orgArea)
    }

    @Log("查询个人项目库")
    @ApiOperation("查询个人项目库")
    @GetMapping("wxme")
    fun querywxMe(criteria: ProjectMeCriteria, pageable: Pageable): PageDTO<ProjectDto> {
        return projectService.queryWxMe(criteria , pageable)
    }

    @Log("查询个人项目库网页")
    @ApiOperation("查询个人项目库网页")
    @GetMapping("me")
    fun queryMe(criteria: ProjectMeQueryCriteria, pageable: Pageable): PageDTO<ProjectDto> {
        return projectService.queryMe(criteria , pageable)
    }

    @AnonymousGetMapping("id/{id}")
    @Log("查询单个项目库")
    @ApiOperation("查询单个项目库")
    fun queryOne(@PathVariable("id") id: String): ProjectDto {
        return projectService.findById(id)
    }

    @PostMapping
    @Log("新增项目库")
    @ApiOperation(value = "新增项目库")
    fun create(@Validated @RequestBody resources: ProjectDto): ProjectDto {
        return projectService.create(resources, JwtSecurityUtils.hasAuthority("project:add"))
    }

    @Log("修改项目库")
    @ApiOperation("修改项目库")
    @PutMapping
    fun updateProject(@Validated @RequestBody resources: Project): ProjectDto {
        return projectService.update(resources, JwtSecurityUtils.hasAuthority("project:edit"))
    }


    @DeleteMapping
    @Log("删除项目库")
    @ApiOperation("删除项目库")
    fun deleteProject(@RequestBody ids: Array<String>) {
        projectService.deleteAll(ids)
    }

    @PostMapping("/getQRcode")
    @Log("项目库详情生成二维码")
    @ApiOperation("项目库详情生成二维码")
    fun getQRcode(@Validated @RequestBody projectQRCodeDto: ProjectQRCodeDto): String {
        return projectService.getQRcode(projectQRCodeDto.id)
    }
}